package com.masterclient.app.modules.goods.contract;

import com.eoner.baselib.presenter.ThrowableConsumer;
import com.masterclient.app.httpservice.RetrofitUtil2;
import com.eoner.baselibrary.bean.goods.CommentBean;
import com.eoner.baselibrary.bean.goods.CommentTabBean;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class CommentPresenter extends CommentContract.Presenter{

    @Override
    public void getCommentList(String product_id, String type,String page) {
        Observable<CommentBean> observable = RetrofitUtil2.getInstance().getCommentList(product_id,type,page);

        Disposable disposable = toSubscribe(observable, new Consumer<CommentBean>() {
            @Override
            public void accept(@NonNull CommentBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.showGoodsComment(bean.getData());
                }else{
                    mView.getCommentListFail(bean.getMsg());
                }
            }
        }, new ThrowableConsumer(){
            @Override
            public void accept(Throwable o) throws Exception {
                super.accept(o);
                mView.getCommentListFail("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }

    @Override
    public void getTabData(String product_id) {

        Observable<CommentTabBean> observable = RetrofitUtil2.getInstance().getCommentTab(product_id);
        Disposable disposable = toSubscribe(observable, new Consumer<CommentTabBean>() {
            @Override
            public void accept(@NonNull CommentTabBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.getTabDataSuccess(bean.getData().getTab_info());
                }else {
                    mView.getTabDataFail(bean.getMsg());
                }
            }
        },new ThrowableConsumer(){
            @Override
            public void accept(@NonNull Throwable o) throws Exception {
                super.accept(o);
                mView.getTabDataFail("网络异常，请重试");
            }
        });
        mRxManager.add(disposable);
    }
}